package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.heima.api.article.ApArticleControllerApi;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.search.vos.SearchArticleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author admin
 */
@RestController
@RequestMapping("/api/v1/article")
public class ApArticleController implements ApArticleControllerApi {

    @Resource
    private ApArticleService articleService;

    /**
     * @param articleDto 文章内容对象
     * @return 保存结果
     * @Description 保存app文章
     */
    @Override
    @PostMapping("/save")
    public ResponseResult saveArticle(@RequestBody ArticleDto articleDto) {
        return articleService.saveArticle(articleDto);
    }

    @GetMapping("{id}")
    @Override
    public SearchArticleVo findArticle(@PathVariable Long id) {
        ApArticle article = articleService.getById(id);
        if (!ObjectUtils.isEmpty(article)) {
            SearchArticleVo searchArticleVo = new SearchArticleVo();
            BeanUtils.copyProperties(article, searchArticleVo);
            return searchArticleVo;
        }
        return null;
    }
}